﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using EnvDTE;
using EnvDTE80;

namespace SinacorRules
{
    public sealed class Rule_ProjectDetails : BaseAddIn.Rules.BaseRuleAddIn
    {
        public Rule_ProjectDetails()
            : base("ProjectDetails",
                   "SinacorRules.SinacorRules",
                   typeof(Rule_ProjectDetails).Assembly)
        {
        }

        protected sealed override void Check(Project projeto)
        {
            string[] aux;
            string defaultNamespace = null;
            string outputFileName = null;
            FileInfo arquivo = new FileInfo(projeto.FullName);

            bool erro;

            defaultNamespace = projeto.Properties.Item("DefaultNamespace").Value.ToString();
            outputFileName = projeto.Properties.Item("OutputFileName").Value.ToString();

            if (IsSinacorProject(arquivo.Directory, "UI"))
            {
                #region Analisa Nome do Projeto

                erro = false;
                aux = Path.GetFileNameWithoutExtension(projeto.FullName).Split('.');

                if (aux.Length == 5
                    || aux.Length == 6)
                {
                    if (aux[0] != "Sinacor")
                    {
                        erro = true;
                    }
                    else if (aux[1] != "UI")
                    {
                        erro = true;
                    }
                    else if (aux[2] != arquivo.Directory.Parent.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[3] != arquivo.Directory.Parent.Name)
                    {
                        erro = true;
                    }

                    if (aux.Length == 5)
                    {
                        if (aux[4] != "Model"
                           && aux[4] != "Communication"
                           && aux[4] != "Presenter")
                        {
                            erro = true;
                        }
                    }
                    else
                    {
                        if (aux[4] != "View"
                            && aux[5] != "SmartClient")
                        {
                            erro = true;
                        }
                    }
                }
                else
                {
                    erro = true;
                }

                if (erro)
                    GeraProblemaAddIn(projeto.FullName,
                                         "UIProjectNameResolution",
                                         projeto.FullName);

                #endregion

                #region Analisa Default NameSpace

                erro = false;

                aux = defaultNamespace.Split('.');

                if (aux.Length == 5
                    || aux.Length == 6)
                {
                    if (aux[0] != "Sinacor")
                    {
                        erro = true;
                    }
                    else if (aux[1] != "UI")
                    {
                        erro = true;
                    }
                    else if (aux[2] != arquivo.Directory.Parent.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[3] != arquivo.Directory.Parent.Name)
                    {
                        erro = true;
                    }

                    if (aux.Length == 5)
                    {
                        if (aux[4] != "Model"
                           && aux[4] != "Communication"
                           && aux[4] != "Presenter")
                        {
                            erro = true;
                        }
                    }
                    else
                    {
                        if ((aux[4] != "View"
                             && aux[5] != "SmartClient")
                            || (aux[4] != "Model"
                             && aux[5] != "Enum"))
                        {
                            erro = true;
                        }
                    }
                }
                else
                {
                    erro = true;
                }

                if (erro)
                    GeraProblemaAddIn(projeto.FullName,
                                     "UIDefaultNameSpaceResolution",
                                     projeto.FullName);

                #endregion

                #region Analisa Output Name

                erro = false;
                aux = outputFileName.Split('.');

                if (aux.Length == 6
                    || aux.Length == 7)
                {
                    if (aux[0] != "Sinacor")
                    {
                        erro = true;
                    }
                    else if (aux[1] != "UI")
                    {
                        erro = true;
                    }
                    else if (aux[2] != arquivo.Directory.Parent.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[3] != arquivo.Directory.Parent.Name)
                    {
                        erro = true;
                    }

                    if (aux.Length == 6)
                    {
                        if (aux[4] != "Model"
                           && aux[4] != "Communication"
                           && aux[4] != "Presenter")
                        {
                            erro = true;
                        }
                    }
                    else
                    {
                        if (aux[4] != "View"
                            && aux[5] != "SmartClient")
                        {
                            erro = true;
                        }
                    }
                }
                else
                {
                    erro = true;
                }

                if (erro)
                    GeraProblemaAddIn(projeto.FullName,
                                     "UIOutputNameResolution",
                                     projeto.FullName);

                #endregion
            }
            else if (IsSinacorProject(arquivo.Directory, "Servicos"))
            {
                #region Analisa Nome do Projeto

                erro = false;
                aux = Path.GetFileNameWithoutExtension(projeto.FullName).Split('.');

                if (aux.Length == 5)
                {
                    if (aux[0] != "Sinacor")
                    {
                        erro = true;
                    }
                    else if (aux[1] != "Servico")
                    {
                        erro = true;
                    }
                    else if (aux[2] != arquivo.Directory.Parent.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[3] != arquivo.Directory.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[4] != "BusinessEntity"
                             && aux[4] != "BusinessLogic"
                             && aux[4] != "ResourceAccess"
                             && aux[4] != "Service")
                    {
                        erro = true;
                    }
                }
                else
                {
                    erro = true;
                }

                if (erro)
                    GeraProblemaAddIn(projeto.FullName,
                                     "ServiceProjectNameResolution",
                                     projeto.FullName);

                #endregion

                #region Analisa Default NameSpace

                erro = false;
                aux = defaultNamespace.Split('.');

                if (aux.Length == 5
                    || aux.Length == 6
                    || aux.Length == 7)
                {
                    if (aux[0] != "Sinacor")
                    {
                        erro = true;
                    }
                    else if (aux[1] != "Servico")
                    {
                        erro = true;
                    }
                    else if (aux[2] != arquivo.Directory.Parent.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[3] != arquivo.Directory.Parent.Name)
                    {
                        erro = true;
                    }

                    if (aux.Length == 5)
                    {
                        if (aux[4] != "BusinessEntity"
                            && aux[4] != "BusinessLogic"
                            && aux[4] != "ResourceAccess"
                            && aux[4] != "Service")
                        {
                            erro = true;
                        }
                    }
                    else if (aux.Length == 6)
                    {
                        if ((aux[4] != "BusinessEntity"
                                && aux[5] != "Enum")
                             || (aux[4] != "BusinessEntity"
                                 && aux[5] != "Parameter")
                             || (aux[4] != "Service"
                                 && aux[5] != "ServiceContract")
                             || (aux[4] != "Service"
                                 && aux[5] != "ServiceImplementation")
                             || (aux[4] != "Service"
                                 && aux[5] != "DataContract"))
                        {
                            erro = true;
                        }
                    }
                    else
                    {
                        if ((aux[4] != "Service"
                                 && aux[5] != "ServiceImplementation"
                                 && aux[6] != "Translator")
                             || (aux[4] != "Service"
                                 && aux[5] != "DataContract"
                                 && aux[6] != "Request")
                             || (aux[4] != "Service"
                                 && aux[5] != "DataContract"
                                 && aux[6] != "Response")
                             || (aux[4] != "Service"
                                 && aux[5] != "DataContract"
                                 && aux[6] != "Enum"))
                        {
                            erro = true;
                        }
                    }
                }
                else
                {
                    erro = true;
                }

                if (erro)
                    GeraProblemaAddIn(projeto.FullName,
                                                 "ServiceDefaultNameSpaceResolution",
                                                 projeto.FullName);

                #endregion

                #region Analisa Output Name

                erro = false;
                aux = outputFileName.Split('.');

                if (aux.Length == 6)
                {
                    if (aux[0] != "Sinacor")
                    {
                        erro = true;
                    }
                    else if (aux[1] != "Servico")
                    {
                        erro = true;
                    }
                    else if (aux[2] != arquivo.Directory.Parent.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[3] != arquivo.Directory.Parent.Name)
                    {
                        erro = true;
                    }
                    else if (aux[4] != "BusinessEntity"
                             && aux[4] != "BusinessLogic"
                             && aux[4] != "ResourceAccess"
                             && aux[4] != "Service")
                    {
                        erro = true;
                    }
                }
                else
                {
                    erro = true;
                }

                if (erro)
                    GeraProblemaAddIn(projeto.FullName,
                                       "ServiceOutputNameResolution",
                                       projeto.FullName);

                #endregion
            }
        }

        private bool IsSinacorProject(DirectoryInfo pasta, string type)
        {
            bool retorno = false;

            if (pasta != null
                && pasta.Parent != null
                && pasta.Parent.Parent != null
                && pasta.Parent.Parent.Parent != null
                && pasta.Parent.Parent.Parent.Name == type)
            {
                retorno = true;
            }

            return retorno;
        }
    }
}
